package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberAuthDO;
import com.bootdo.wx.domain.MemberDoctorDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberAuthService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-08-04 14:23:03
 */
 
@Controller
@RequestMapping("/wx/memberAuth")
public class MemberAuthController {
	@Autowired
	private MemberAuthService memberAuthService;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@GetMapping()
	@RequiresPermissions("wx:memberAuth:memberAuth")
	String MemberAuth(){
	    return "wx/memberAuth/memberAuth";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberAuth:memberAuth")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
		//查询列表数据
        Query query = new Query(params);
		List<MemberAuthDO> memberAuthList = memberAuthService.list(query);
		if(memberAuthList.size()>0){
			for (MemberAuthDO memberAuthDO : memberAuthList) {
				MemberUserDO memberUserDO = memberUserDao.get(memberAuthDO.getUserId());
				if(memberUserDO!=null){
					memberAuthDO.setUserId(memberUserDO.getAvator());
				}
			}
		}
		int total = memberAuthList.size();
		PageUtils pageUtils = new PageUtils(memberAuthList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberAuth:add")
	String add(){
	    return "wx/memberAuth/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberAuth:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberAuthDO memberAuth = memberAuthService.get(id);
		model.addAttribute("memberAuth", memberAuth);
	    return "wx/memberAuth/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberAuth:add")
	public R save( MemberAuthDO memberAuth){
	    memberAuth.setId(UUIDUtils.getUUID());
	    memberAuth.setDelFlag(0);
	    memberAuth.setCaAt(new Date());
		if(memberAuthService.save(memberAuth)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberAuth:edit")
	public R update( MemberAuthDO memberAuth){
		memberAuth.setUpAt(new Date());
		memberAuthService.update(memberAuth);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberAuth:remove")
	public R remove( String id){
		MemberAuthDO memberAuth = memberAuthService.get(id);
		memberAuth.setDelFlag(1);
		memberAuth.setUpAt(new Date());
		if(memberAuthService.update(memberAuth)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberAuth:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MemberAuthDO memberAuth = memberAuthService.get(string);
				memberAuth.setDelFlag(1);
				memberAuth.setUpAt(new Date());
				memberAuthService.update(memberAuth);
			}
		}
	
		return R.ok();
	}
	
	/***
	 * 
	 * @author long
	 * 2020年6月15日下午5:51:21
	 * 方法释义:审核医生
	 * 参数:
	 *
	 */
	@ResponseBody
	@PostMapping("/shen")
	@RequiresPermissions("wx:memberAuth:edit")
	public R shen(MemberAuthDO memberAuth){
		return memberAuthService.shen(memberAuth);
	}
	
}
